package ua.kpi.fpm.km61.strat.sp.sa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.TreeSet;

import ua.kpi.fpm.km61.strat.sp.la.MarkedString;
import ua.kpi.fpm.km61.strat.sp.la.ResString;
import ua.kpi.fpm.km61.strat.sp.rules.Rule;
import ua.kpi.fpm.km61.strat.sp.rules.Terminal;
import ua.kpi.fpm.km61.strat.sp.sa.tables.ActionResult;
import ua.kpi.fpm.km61.strat.sp.sa.tables.ActionTable;
import ua.kpi.fpm.km61.strat.sp.sa.tables.GotoTable;
import ua.kpi.fpm.km61.strat.sp.sa.tables.State;
import ua.kpi.fpm.km61.strat.sp.util.FileProcessor;

public class Parser {

	private Grammar grammar;

	private List<MarkedString> code;

	private ActionTable actionTable;

	private GotoTable gotoTable;

	private Stack<Terminal> stack;

	public Parser() {
		this.grammar = new Grammar();
		this.actionTable = TableGenerator.generateActionTable();
		this.gotoTable = TableGenerator.generateGotoTable();
	}

	public Grammar getGrammar() {
		return grammar;
	}

	public void setGrammar(Grammar grammar) {
		this.grammar = grammar;
	}

	public List<MarkedString> getCode() {
		return code;
	}

	public void setCode(List<MarkedString> code) {
		this.code = code;
	}

	public void parse(TreeSet<ResString> code1) {
		List<Terminal> code = new ArrayList<Terminal>();
		for (ResString rs : code1) {
			if (rs.getLex().equals("ids")) {
				Terminal term = new Terminal("id");
				code.add(term);
			}
			if (rs.getLex().equals("dou")) {
				Terminal term = new Terminal("dou");
				code.add(term);
			}
			if (rs.getLex().equals("int")) {
				Terminal term = new Terminal("int");
				code.add(term);
			}
			if (rs.getStr().equals("true")
					|| rs.getStr().equals("false")) {
				Terminal term = new Terminal("bool");
				code.add(term);
			}
			if (rs.getStr().equals("null")) {
				Terminal term = new Terminal("null");
				code.add(term);
			}
			if (rs.getLex().equals("res")) {
				if (rs.getStr().equals("private")
						|| rs.getStr().equals("public")) {
					Terminal term = new Terminal("mod");
					code.add(term);
				}
				if (rs.getStr().equals("int")
						|| rs.getStr().equals("float") || rs.getStr().equals("boolean")) {
					Terminal term = new Terminal("prim");
					code.add(term);
				}
			}
		}
		while (true) {
			Iterator<Terminal> iter = code.iterator();
			Terminal a = iter.next();
			//this.actionTable.analyse(stack.pop(), a);
			switch (this.actionTable.getRes()) {
			case S: {

			}
			case R: {

			}
			case ACC: {

			}
			case ERROR: {

			}
			}
		}
	}
}
